﻿/*
   绑定视图：Login.xaml
   文件说明：用于控制用户登录/退出
*/


using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZFS.Library.Helper;
using ZFSDomain.Common.CoreLib;
using ZFSDomain.Interface.User;
using ZFSDomain.Service;
using ZFSInterface.User;

namespace ZFSDomain.ViewModel.Sign
{

    /// <summary>
    /// 登录逻辑处理
    /// </summary>
    public class LoginViewModel : ViewModelBase
    {
        #region 用户名/密码

        private string userName = string.Empty;
        private string passWord = string.Empty;

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get { return userName; }
            set { userName = value; RaisePropertyChanged(); }
        }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password
        {
            get { return passWord; }
            set { passWord = value; RaisePropertyChanged(); }
        }

        #endregion

        #region 命令(Binding Command)

        private RelayCommand _signCommand;

        public RelayCommand SignCommand
        {
            get
            {
                if (_signCommand == null)
                {
                    _signCommand = new RelayCommand(() => Login());
                }
                return _signCommand;
            }
            set { _signCommand = value; RaisePropertyChanged(); }
        }

        private RelayCommand _exitCommand;

        public RelayCommand ExitCommand
        {
            get
            {
                if (_exitCommand == null)
                {
                    _exitCommand = new RelayCommand(() => Exit());
                }
                return _exitCommand;
            }
            set { _exitCommand = value; RaisePropertyChanged(); }
        }

        #endregion

        /// <summary>
        /// 登陆系统
        /// </summary>
        public async void Login()
        {
            try
            {
                if (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password))
                {
                    IUser user = ZFSBridge.BridgeFactory.GetUserBridge();
                    var req = await Task.Run(() => user.Login(UserName, Password));
                    if (req != null)
                    {
                        Messenger.Default.Send("", "Hide");
                        Loginer.LoginerUser.Account = req.Account;
                        Loginer.LoginerUser.UserName = req.UserName;
                        Loginer.LoginerUser.IsAdmin = req.FlagAdmin == "1" ? true : false;
                        Loginer.LoginerUser.Email = req.Email;
                        Loginer.LoginerUser.UserAuthority = await Task.Run(() => ZFSBridge.BridgeFactory.
                             GetUserBridge().GetAuthority(Loginer.LoginerUser.Account));

                        MainViewModel model = new MainViewModel();
                        model.PopBoxView = new Step.PopBoxViewModel();
                        model.LoginerInfo = new LoginerInfo() { UserName = req.UserName, Account = req.Account };
                        model.NoticeView = new Step.NoticeViewModel();
                        model.LoadMenu();
                        model.InitDefaultView();
                        var dialog = ServiceProvider.Instance.Get<IMainViewDialog>();
                        dialog.BindViewModel(model);
                        dialog.ShowDialog();
                        this.Exit();
                    }
                    else
                        Msg.Info("用户名或密码错误！");
                }
            }
            catch (Exception ex)
            {
                Msg.Error(ex);
            }

        }

        /// <summary>
        /// 关闭系统
        /// </summary>
        public void Exit()
        {
            Messenger.Default.Send("", "Exit");
        }
    }


}
